<template>
  <div class="container">
    <div class="header">
      <el-button
        class="addCuisine"
        icon="el-icon-plus"
        @click="addClass('cuisine')"
        >新增菜单分类</el-button
      >
      <el-button
        class="addInclusive"
        icon="el-icon-plus"
        @click="addClass('inclusive')"
        >新增套餐分类</el-button
      >
    </div>
    <template>
      <el-table :data="tableData">
        <el-table-column prop="name" label="分类名称" align="center">
        </el-table-column>
        <el-table-column prop="type" label="分类类型" align="center">
          <template v-slot="{ row }">
            {{ row.type === 1 ? '菜品分类' : '套餐分类' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="操作时间" align="center">
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-link type="primary" @click="handleEdit(row)">修改</el-link>
            <el-divider direction="vertical"></el-divider>
            <el-link type="danger" @click="handleDelete(row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table></template
    >

    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="classData.title"
      :visible.sync="classData.dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form class="demo-form-inline" label-width="100px">
        <el-form-item label="分类名称：">
          <el-input
            v-model="classData.name"
            placeholder="请输入分类名称"
            maxlength="14"
          />
        </el-form-item>
        <el-form-item label="排序：">
          <el-input
            v-model="classData.sort"
            type="number"
            placeholder="请输入排序"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="classData.dialogVisible = false"
          >取 消</el-button
        >
        <el-button
          size="medium"
          type="primary"
          @click="submitForm()"
          class="ok-btn"
          >确 定</el-button
        >
        <el-button
          type="primary"
          v-if="action !== 'edit'"
          @click="submitForm('str')"
          class="save-btn"
          >保存并继续添加</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getClassified,
  delclassified,
  addCategory,
  editCategory
} from '@/api/classified'
export default {
  name: 'ClassiFied',
  data () {
    return {
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
      action: '',
      classData: {
        title: '新增菜品分类',
        dialogVisible: false,
        categoryId: '',
        name: '',
        sort: ''
      }
    }
  },
  created () {
    this.init()
  },

  methods: {
    init () {
      getClassified({ ...this.pageInfo }).then(res => {
        const { records, total } = res.data
        this.tableData = records
        this.total = total
      })
    },
    handleSizeChange (val) {
      this.pageInfo.page = 1
      this.pageInfo.pageSize = val
      this.init()
    },
    handleCurrentChange (val) {
      this.pageInfo.page = val
      this.init()
    },
    addClass (str) {
      this.action = 'add'
      this.classData.name = ''
      this.classData.sort = ''
      this.classData.dialogVisible = true
      if (str === 'cuisine') {
        this.classData.title = '新增菜单分类'
        this.type = '1'
      } else {
        this.classData.title = '新增套餐分类'
        this.type = '2'
      }
    },
    handleEdit (data) {
      this.classData.title = '修改分类'
      this.action = 'edit'
      this.classData.name = data.name
      this.classData.sort = data.sort
      this.classData.id = data.id
      this.classData.dialogVisible = true
    },
    handleDelete (id) {
      this.$confirm('确定删除该分类吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delclassified({ id }).then(res => {
          this.$success('删除成功')
          this.init()
        })
      })
    },
    handleClose (str) {
      this.classData.dialogVisible = false
    },
    submitForm (str) {
      const classData = this.classData
      const valid =
        (classData.name === 0 || classData.name) &&
        (classData.sort === 0 || classData.sort)
      if (this.action === 'add') {
        if (valid) {
          const reg = /^\d+$/
          if (reg.test(classData.sort)) {
            addCategory({
              name: classData.name,
              type: this.type,
              sort: classData.sort
            }).then(res => {
              console.log(res)
              if (res.code === 1) {
                this.$message.success('分类添加成功！')
                if (!str) {
                  this.classData.dialogVisible = false
                } else {
                  this.classData.name = ''
                  this.classData.sort = ''
                }
                this.init()
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          } else {
            this.$message.error('排序只能输入数字类型')
          }
        } else {
          this.$message.error('请输入分类名称或排序')
        }
      } else if (valid) {
        const reg = /^\d+$/
        if (reg.test(this.classData.sort)) {
          editCategory({
            id: this.classData.id,
            name: this.classData.name,
            sort: this.classData.sort
          }).then(res => {
            if (res.code === 1) {
              this.$message.success('分类修改成功！')
              this.classData.dialogVisible = false
              this.init()
            } else {
              this.$message.error(res.msg || '操作失败')
            }
          })
        } else {
          this.$message.error('排序只能输入数字类型')
        }
      } else {
        this.$message.error('请输入分类名称或排序')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../styles/variables.scss';
.container {
  padding: 30px 28px;
  .header {
    margin-bottom: 20px;
    .addCuisine {
      background-color: $darkblack;
      color: #fff;
      &:hover {
        background-color: $lightblack;
      }
    }
    .addInclusive {
      background-color: $darkorange;
      color: $darkblack;
      &:hover {
        background-color: $lightorange;
      }
    }
  }
  .footer {
    display: flex;
    justify-content: center;
    margin-top: 30px;
  }
}
</style>
